﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight.Threading;
using Hammock;
using Hammock.Web;
using Microsoft.Phone.Controls;
using MyMeetup.ViewModels;
using Newtonsoft.Json;

namespace MyMeetup
{
    public partial class PanoramaGroupPage : PhoneApplicationPage
    {
        protected IGroup _group;


        public PanoramaGroupPage()
        {
            InitializeComponent();
          
        }

       
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //var url = e.Uri.ToString();
            //string groupId;
            //NavigationContext.QueryString.TryGetValue("groupId", out groupId);

            

            //GetGroups(groupId, (x) => { var vm = GlobalViewModelLocator.GroupPageViewModelStatic;
            //    vm.Model = x[0];
             
            //    DataContext = vm;
            //});

            
            //base.OnNavigatedTo(e);
            //if (_group == null && State.ContainsKey("SelectedGroup"))
            //    _group = State["SelectedGroup"] as Group;

        }

        protected void NetworkErrorCallback(RestRequest request, RestResponse response, object userState)
        {
            Deployment.Current.Dispatcher.BeginInvoke(
                () => MessageBox.Show("There was a problem connecting to meetup.com.",
                                      "Network Error", MessageBoxButton.OK));
        }

       

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            State["SelectedGroup"] = _group;
        }


        
        private void CheckIn(object sender, RoutedEventArgs e)
        {
            if (LicenseHelper.IsTrial())
            {
                NavigationService.Navigate(new Uri("/BuyNow.xaml", UriKind.Relative));
            }
            else
            {
                var currentEvent = ((MenuItem) sender).DataContext as Event;
                currentEvent.CheckinComplete += new CheckinCompleteEventHandler(CurrentEventCheckinComplete);
                currentEvent.Checkin();
            }
        }

        void CurrentEventCheckinComplete(object sender, CheckinEventArgs e)
        {
            if (e.Success)
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show("Checkin successful",
                                                             "Thank You!",
                                                             MessageBoxButton
                                                                 .OK));
            }
            else
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show(e.Message));
            }
            ((Event)sender).CheckinComplete -= CurrentEventCheckinComplete;
        }

        private void RsvpYes(object sender, RoutedEventArgs e)
        {
            var currentEvent = ((MenuItem)sender).DataContext as Event;
            PostRsvp(currentEvent, RSVPType.Yes);
        }

        private void RsvpNo(object sender, RoutedEventArgs e)
        {
            var currentEvent = ((MenuItem)sender).DataContext as Event;
            PostRsvp(currentEvent, RSVPType.No);
        }

        private void PostRsvp(Event currentEvent, RSVPType rsvp)
        {
            if (LicenseHelper.IsTrial())
            {
                NavigationService.Navigate(new Uri("/BuyNow.xaml", UriKind.Relative));
            }
            else
            {
                currentEvent.RsvpComplete += CurrentEventRsvpComplete;
                currentEvent.Rsvp(rsvp);
            }
        }

        void CurrentEventRsvpComplete(object sender, RsvpEventArgs e)
        {
            if (e.Success)
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show(e.Message,
                                                             "Successful RSVP",
                                                             MessageBoxButton
                                                                 .OK));
            }
            else
            {
                Dispatcher.BeginInvoke(() => MessageBox.Show(e.Message));
            }

            ((Event)sender).RsvpComplete -= CurrentEventRsvpComplete;
        }

    }
}